from langgraph.func import task, entrypoint
from langgraph.types import RetryPolicy
from langgraph.checkpoint.memory import MemorySaver
from langgraph.store.memory import InMemoryStore,BaseStore
from langgraph.types import StreamWriter
from langchain_core.runnables import RunnableConfig
from typing import Any

stoer = InMemoryStore()
checkpoint = MemorySaver()

retry_policy = RetryPolicy(max_attempts=2,retry_on=TimeoutError)

@task(name='api_data_fetcher',retry_policy=retry_policy)
def api_data_fetcher(api_endpoint:str) -> dict:
    """从带有重试策略的api断点获取数据任务"""
    import time,random
    time.sleep(random.random())
    if random.random() < 0.8:
        print("process...")
        raise TimeoutError("API请求超时")
    return {"status":"success","data":f"来自{api_endpoint}的数据"}

@entrypoint(checkpointer=MemorySaver())
def data_processing_workflow(endpoint_url:str) -> dict:
    """"""
    api_result = api_data_fetcher(api_endpoint=endpoint_url).result()
    return {"workflow_result":"数据已处理","api_response":api_result}

config = {"configurable":{"thread_id":"jajdd001"}}
result = data_processing_workflow.invoke("http://www.sina.com.cn",config)
print(result)